package Entity;

import java.util.List;
import java.util.Locale;

public class Robot {
    private String iconPath;
    // 机器人标识
    private String id;
    // 机器人位置
    private double x;
    private double y;
    // 机器人状态
    private RobotStatus status;
    // 电池电量

    private int battery;
    // 速度
    private double speed;
    // 有效负载重量
    private int payload;
    private int moveDelay = 0;  // 新增: 用于控制暂停轮数
    // 机器人图标地址
    private int PutdownIndex;//在第几个点putdown
    private int PickupIndex;//在第几个点pickup
    private boolean carryingProduct;
    private List<int[]> path;//目标路径
    int CurrentIndex;//现在移动到路径中第几个点了
    public Products product;
    public boolean isimportorder;
    public ChargingStations chargingStations;//只有订单需要特殊处理
    public Robot(String id, int x, int y, RobotStatus status, int battery, double speed, int payload, String iconPath,ChargingStations station) {
        this.id = id;
        this.x = x;
        this.y = y;
        this.status = status;
        this.battery = battery;
        this.speed = speed;
        this.payload = payload;
        this.iconPath = iconPath;
        PutdownIndex = 0;
        PickupIndex = 0;
        path = null;
        carryingProduct = false;
        CurrentIndex = 0;
        product=null;
        isimportorder=false;
        chargingStations=station;
    }

    // 传入最短路径移动
    public void move() {
        if (moveDelay > 0) {
            moveDelay--;  // 如果正在暂停，则减少暂停轮数
            return;  // 跳过本轮移动
        }
        if (CurrentIndex == PutdownIndex) {
            putdown();  // 执行 putdown

        }
        // 获取当前目标点
        if (CurrentIndex == path.size()) return;
        if (path.isEmpty()) return;
        int[] target = path.get(CurrentIndex);

        // 计算方向
        double dx = target[0] - this.x;
        double dy = target[1] - this.y;

        // 移动
        if (Math.abs(dx) <= speed && Math.abs(dy) <= speed) {
            // 如果接近目标点，直接到达并从列表中移除该点
            this.x = target[0];
            this.y = target[1];
            if(this.battery>=0)
            this.battery-=1;
            CurrentIndex++; // 移除已到达的目标点
            this.battery--;
        } else {
            // 按比例移动
            double distance = Math.sqrt(dx * dx + dy * dy);
            this.x += speed * dx / distance;
            this.y += speed * dy / distance;
        }
    }

    // 根据机器人的电量和状态确定机器人图标地址
    public String getIconPathByBattery() {
        String iconPath = "";
        String id = this.id.toLowerCase(Locale.ROOT);
        String color;
        // 判断电量
        if (this.battery > 50) {
            color = "green";
        } else if (this.battery > 20) {
            color = "orange";
        } else {
            color = "red";
        }
        // 判断状态
        if (this.carryingProduct == true) {
            iconPath = "src\\View\\images\\" + id + "-" + color + "-" + "black" + ".png";
        } else if (this.status == RobotStatus.INACTIVE) {
            iconPath = "src\\View\\images\\" + id + "-" + color + ".png";
        }
        return iconPath;
    }

    public double getY() {
        return y;
    }

    public void setY(double y) {
        this.y = y;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public double getX() {
        return x;
    }

    public void setX(double x) {
        this.x = x;
    }

    public RobotStatus getStatus() {
        return status;
    }

    public void setStatus(RobotStatus status) {
        this.status = status;
    }

    public int getBattery() {
        return battery;
    }

    public void setBattery(int battery) {
        this.battery = battery;
    }

    public double getSpeed() {
        return speed;
    }

    public void setSpeed(double speed) {
        this.speed = speed;
    }

    public int getPayload() {
        return payload;
    }

    public void setPayload(int payload) {
        this.payload = payload;
    }

    public String getIconPath() {
        return iconPath;
    }

    public void setIconPath(String iconPath) {
        this.iconPath = iconPath;
    }

    public void setPath(List<int[]> path) {
        this.path = path;
    }

    public List<int[]> getPath() {
        return path;
    }

    public void pickup() {
        this.moveDelay = 5;
        System.out.print(id + " ");
        System.out.println("has pick up");
        carryingProduct = true;
    }

    public boolean isCarryingProduct() {
        return carryingProduct;
    }

    // 放下产品
    public void putdown() {

        this.moveDelay = 5;
        System.out.print(id + " ");
        System.out.println("has put down");
        this.status = RobotStatus.INACTIVE;
        carryingProduct = false;
    }

    public int getCurrentIndex() {
        return CurrentIndex;
    }

    public int getPutdownIndex() {
        return PutdownIndex;
    }

    public int getPickupIndex() {
        return PickupIndex;
    }

    public void setPickupIndex(int pickupIndex) {
        PickupIndex = pickupIndex;
    }

    public void setPutdownIndex(int putdownIndex) {
        PutdownIndex = putdownIndex;
    }

    public void setCurrentIndex(int i) {
        CurrentIndex = i;
    }
}
